<?php
namespace Admin\Controller;
use Think\Controller;
class ADController  extends IndexController{
    public function index(){
        $this->display();
        }
    public function scancode(){
        if (I("request.iDisplayStart") == '') {
            $start = 0;
        } else {
            $start = I("request.iDisplayStart");
        }
        //===========================时间判断====================
        date_default_timezone_set('Asia/Shanghai');
        //获取时间，修改日期格式 如：2014-04-02 改成 2014/06/07
        if (I("request.data_time_one") != "") {
            $data_time_one = strtotime(I("request.data_time_one"));
        }
        if (I("request.data_time_two") != "") {
            $data_time_two = strtotime(I("request.data_time_two"));
        }
        if($data_time_one=="" AND $data_time_two==""){
            $map['s_time']= array('LT',time()) ;

        }elseif($data_time_one!="" AND $data_time_two==""){
            $map['s_time'] = array('EGT',$data_time_one) ;

        }elseif($data_time_one=="" AND $data_time_two!=""){
            $map['s_time'] = array('EGT',$data_time_two) ;

        }elseif( $data_time_one!="" AND $data_time_two!="" AND $data_time_one == $data_time_two) {

             $data_time_two = $data_time_two + (24 * 3599);
             $map['s_time'] = array(array('EGT',$data_time_one),array('LT',$data_time_two)) ;
            //sql $data_time_one >=s_time AND s_time <= $data_time_two
        } elseif($data_time_one < $data_time_two) {
            $data_time_two = $data_time_two + (24 * 3599);
            $map['s_time'] = array(array('GT',$data_time_one),array('LT',$data_time_two)) ;

        }elseif($data_time_one >$data_time_two){
             $data_time_one = $data_time_one + (24 * 3599);
            $map['s_time'] = array(array('LT',$data_time_one),array('EGT',$data_time_two)) ;
        }
        //判断二维码类型
        if ((I("request.data_code") != -1) && (I("request.data_code") != '')) {
           $map['qrcode']= I("request.data_code");

        }



        //取数据
        $Test=D("Test");
        $testCount = $Test->where($map)->count();
       
        $res = $Test->where($map)->order("s_time DESC")->limit($start,I("request.iDisplayLength"))->select();
        if(!$res){
            
             $result = array(
            "aaData" =>   $res
        );
            echo json_encode($result); exit;
        }

        
        foreach($res as $key=>&$value){
            $value['s_time']=$value['s_time']!="" ? date("Y/m/d H:i:s",$value['s_time']):"";
        }

        $result = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $start,
            "iTotalDisplayRecords" =>0,
            "aaData" => $res
        );

        echo json_encode($result);exit;
    }

    // 获取二维码列表
    public function code_list()  {
        $Test = D("Test");
        $qrcode = $Test->field('qrcode')->select();
        $result = array();
        foreach ($qrcode as $key => $value) {
            $data[] = implode(',', $value);
        }
        $result = array_unique($data);

        if (!$result) {
            echo json_encode(0);
            exit;
        } else {
            echo json_encode($result);
            exit;
        }
    }





// 下载excel表格
    public function pushs() {
    
        //===========================时间判断====================
        date_default_timezone_set('Asia/Shanghai');
        //获取时间，修改日期格式 如：2014-04-02 改成 2014/06/07
        if (I("request.data_time_one") != "") {
            $data_time_one = strtotime(I("request.data_time_one"));
        }
        if (I("request.data_time_two") != "") {
            $data_time_two = strtotime(I("request.data_time_two"));
        }

        if($data_time_one=="" AND $data_time_two==""){
            $map['s_time']= array('LT',time()) ;

        }elseif($data_time_one!="" AND $data_time_two==""){
            $map['s_time'] = array('EGT',$data_time_one) ;

        }elseif($data_time_one=="" AND $data_time_two!=""){
            $map['s_time'] = array('EGT',$data_time_two) ;

        }elseif( $data_time_one!="" AND $data_time_two!="" AND $data_time_one == $data_time_two) {

             $data_time_two = $data_time_two + (24 * 3600);
             $map['s_time'] = array(array('EGT',$data_time_one),array('LT',$data_time_two)) ;
            //sql $data_time_one >=s_time AND s_time <= $data_time_two

        } elseif($data_time_one < $data_time_two) {
            $map['s_time'] = array(array('GT',$data_time_one),array('LT',$data_time_two)) ;

        }elseif($data_time_one >$data_time_two){
            $map['s_time'] = array(array('LT',$data_time_one),array('EGT',$data_time_two)) ;
        }
        //判断二维码类型
        if ((I("request.code") != -1) && (I("request.code") != '') && (I("request.code") != "undefined")) {
           $map['qrcode']= I("request.code");
        }


        $Test=D("Test");
        $testCount = $Test->where($map)->count("*");
        $res = $Test->where($map)->order("s_time DESC")->select();
        if(!$res){

            echo "暂无数据" ;exit;
        }

        foreach($res as $key=>&$value){
            $value['s_time']=$value['s_time']!="" ? date("Y/m/d H:i:s",$value['s_time']):"";
        }

       /** Error reporting */
        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);
        define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
        date_default_timezone_set('Asia/Shanghai');

        //引入外部的类
        import("Vendor.phpexcel.PHPExcels");
        import("Vendor.phpexcel.PHPExcel.CachedObjectStorageFactory", APP_PATH, ".php");
        import("Vendor.phpexcel.PHPExcel.Settings", APP_PATH, ".php");
        import("Vendor.phpexcel.PHPExcel.IOFactory", APP_PATH, ".php");

        $Factory = new \PHPExcel_CachedObjectStorageFactory();
        $Settings = new \PHPExcel_Settings();
        $IOF = new \PHPExcel_IOFactory();
        $cacheMethod = $Factory::cache_in_memory_gzip;
        if ($Settings::setCacheStorageMethod($cacheMethod)) {
            echo date('H:i:s'), " 使用 ", $cacheMethod, " 方式下载文件", EOL;
        } else {
            echo date('H:i:s'), "将会使用", $cacheMethod, "默认方式下载文件", EOL;
        }
        // Create new PHPExcel object
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator("呱呱试管最初创建")
            ->setLastModifiedBy("呱呱试管最终修改")
            ->setTitle("Office 2007 XLSX 格式的文档")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("格式的文档  Office 2007 XLSX, 使用了PHPExcel.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("文档下载成功");
        // Create a first sheet
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setCellValue('A1', "id");
        $objPHPExcel->getActiveSheet()->setCellValue('B1', "ip地址");
        $objPHPExcel->getActiveSheet()->setCellValue('C1', "二维码编号");
        $objPHPExcel->getActiveSheet()->setCellValue('D1', "扫码时间");
        $objPHPExcel->getActiveSheet()->setCellValue('E1', "扫描数");
        $objPHPExcel->getActiveSheet()->freezePane('A4');
        // Rows to repeat at top
        $objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);

        foreach ($res as $kk => $vv) {
            $kk += 2;
            $objPHPExcel->getActiveSheet()->setCellValue('A' . $kk, $vv['id'])
                ->setCellValue('B' . $kk, $vv['ips'])
                ->setCellValue('C' . $kk, $vv['qrcode'])
                ->setCellValue('D' . $kk, $vv['s_time'])
                ->setCellValue('E' . 3, $testCount)
                ->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');  //
        }
        $objPHPExcel->setActiveSheetIndex(0);
        $callStartTime = microtime(true);
        $objWriter = $IOF::createWriter($objPHPExcel, 'Excel2007');


        ob_end_clean();  //清空缓存

        header("Pragma: public");
        header("Expires: 0");

        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");

        header("Content-Type:application/force-download");

        header("Content-Type:application/vnd.ms-execl");

        header("Content-Type:application/octet-stream");

        header("Content-Type:application/download");
        $filename = date('ymdHis');
        header("Content-Disposition:attachment;filename=$filename.xls");//设置文件的名称

        header("Content-Transfer-Encoding:binary");

        $objWriter->save('php://output');
        exit;

    }


    // 下载excel表格
    public function pushuser()
    {
        $UUser = D("User");
        $result = $UUser->field("id,name,telephone,register_time")->select();
        foreach ($result as $key => &$value) {
            $value['register_time'] = $value['register_time'] != 0 ? date('Y-m-d H:i:s', $value['register_time']) : '';
        }
            /** Error reporting */
            error_reporting(E_ALL);
            ini_set('display_errors', TRUE);
            ini_set('display_startup_errors', TRUE);
            define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
            date_default_timezone_set('Asia/Shanghai');

            //引入外部的类
            import("Vendor.phpexcel.PHPExcels");
            import("Vendor.phpexcel.PHPExcel.CachedObjectStorageFactory", APP_PATH, ".php");
            import("Vendor.phpexcel.PHPExcel.Settings", APP_PATH, ".php");
            import("Vendor.phpexcel.PHPExcel.IOFactory", APP_PATH, ".php");

            $Factory = new \PHPExcel_CachedObjectStorageFactory();
            $Settings = new \PHPExcel_Settings();
            $IOF = new \PHPExcel_IOFactory();
            $cacheMethod = $Factory::cache_in_memory_gzip;
            if ($Settings::setCacheStorageMethod($cacheMethod)) {
                echo date('H:i:s'), " 使用 ", $cacheMethod, " 方式下载文件", EOL;
            } else {
                echo date('H:i:s'), "将会使用", $cacheMethod, "默认方式下载文件", EOL;
            }
            // Create new PHPExcel object
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel->getProperties()->setCreator("呱呱试管最初创建")
                ->setLastModifiedBy("呱呱试管最终修改")
                ->setTitle("Office 2007 XLSX 格式的文档")
                ->setSubject("Office 2007 XLSX Test Document")
                ->setDescription("格式的文档  Office 2007 XLSX, 使用了PHPExcel.")
                ->setKeywords("office 2007 openxml php")
                ->setCategory("文档下载成功");
            // Create a first sheet
            $objPHPExcel->setActiveSheetIndex(0);
            $objPHPExcel->getActiveSheet()->setCellValue('A1', "id");
            $objPHPExcel->getActiveSheet()->setCellValue('B1', "会员名");
            $objPHPExcel->getActiveSheet()->setCellValue('C1', "电话");
            $objPHPExcel->getActiveSheet()->setCellValue('D1', "注册时间");

            // Freeze panes
            $objPHPExcel->getActiveSheet()->freezePane('A4');
            // Rows to repeat at top
            $objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);


            foreach ($result as $key => $value) {
                $key += 2;
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $key, $value['id'])
                    ->setCellValue('B' . $key, $value['name'])
                    ->setCellValue('C' . $key, $value['telephone'])
                    ->setCellValue('D' . $key, $value['register_time'])
                    ->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');  //
            }
            $objPHPExcel->setActiveSheetIndex(0);
            $callStartTime = microtime(true);
            $objWriter = $IOF::createWriter($objPHPExcel, 'Excel2007');


            ob_end_clean();  //清空缓存

            header("Pragma: public");
            header("Expires: 0");

            header("Cache-Control:must-revalidate,post-check=0,pre-check=0");

            header("Content-Type:application/force-download");

            header("Content-Type:application/vnd.ms-execl");

            header("Content-Type:application/octet-stream");

            header("Content-Type:application/download");
            $filename = date('ymdHis');
            header("Content-Disposition:attachment;filename=$filename.xls");//设置文件的名称

            header("Content-Transfer-Encoding:binary");

            $objWriter->save('php://output');

            exit;

        }

    public  function test(){
        $this->display();

}


}



